package com.zhaotianhao.serviceproduct.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaotianhao.serviceproduct.entity.SpecParam;
import com.zhaotianhao.serviceproduct.mapper.SpecParamMapper;
import com.zhaotianhao.serviceproduct.service.ISpecParamService;
import com.zhaotianhao.serviceproduct.vo.SpecParamVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规格参数组下的参数名 服务实现类
 * </p>
 *
 * @author Z-7
 * @since 2022-04-27
 */
@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements ISpecParamService {

    /**
     * 查询所有的规格参数
     *
     * @return
     */
    @Override
    public List<SpecParam> queryAllSpecParam() {
        return getBaseMapper().list();
    }

    /**
     * @param cid
     * @param groupId
     * @return
     */
    @Override
    public List<SpecParamVO> getSpecParamVoListByCidAndGroupId(Long cid, Long groupId) {
        List<SpecParam> listByCidAndGroupId = getListByCidAndGroupId(cid, groupId);

        ArrayList<SpecParamVO> specParamVos = new ArrayList<>();
        for (SpecParam specParam : listByCidAndGroupId) {
            SpecParamVO specParamVo = toSpecParamVo(specParam);
            specParamVos.add(specParamVo);
        }

        return specParamVos;
    }

    /**
     * 通过cid groupId查询参数规格
     * @param cid
     * @param groupId
     * @return
     */
    private List<SpecParam> getListByCidAndGroupId(Long cid, Long groupId) {
        List<SpecParam> list = list(new QueryWrapper<SpecParam>().eq("cid", cid).eq("group_id", groupId));
        return list;
    }

    private SpecParamVO toSpecParamVo(SpecParam specParam) {
        SpecParamVO specParamVo = new SpecParamVO();
        BeanUtil.copyProperties(specParam,specParamVo);
        return specParamVo;
    }
}
